********************************************************************************
* analyze_hetero.do
* Purpose: Produce heterogeneity tables for Appendix B.
*
* Contains all Appendix B tables EXCEPT Table B6 (extended firm outcomes, in analyze_main.do).
*
* Heterogeneity dimensions covered include:
*   - Market overlap: same_market (acquirer and target in the same CZ x industry)
*   - Firm scope: national vs. local firms (num_cz > 1)
*   - Worker characteristics: wage quartile, tenure, sector
*   - Deal type interactions (merger vs. acquisition)
*   - AKM components: firm FE, worker FE, match effect heterogeneity
*
* Merges market overlap (same_market.dta) and CZ count (firm_year_cz.dta)
* into the matched sample to define heterogeneity subgroups.
*
* Input : $data/worker_matched.dta, $data/firm_matched.dta,
*         $data/same_market.dta, $data/firm_year_cz.dta
* Output: $out/tableB*.csv, $out/FigureB*.pdf
********************************************************************************
*        Appendix B - Heterogeneity Analysis      *
********************************************************************************
set more off

********************************************************************************
							* Main Analysis Sample *
********************************************************************************
use $data/worker_matched, clear

merge 	m:1		DEAL Acquirer 	using $data/same_market,	keep(1 3) keepusing(same_market) nogen
merge	m:1		entid_syn year	using $data/firm_year_cz,	keep(1 3) keepusing(num_cz) nogen
replace	num_cz				= .	if treated == 0 | t ~= -1
replace	same_market			= .	if treated == 0 | t ~= -1

gegen matched_same_market	= firstnm(same_market),	by(pairid year_prior)
gegen matched_num_cz		= firstnm(num_cz), 		by(pairid year_prior)

gen 	matched_national 	= 1 if matched_same_market == 0 &  matched_num_cz > 1
replace matched_national 	= 0 if matched_same_market == 1
gen		matched_local		= 1 - matched_national

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B1 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_national == 1
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B1 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_national == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B1 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_national == 0
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B1 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_national == 0
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "National")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Local")";

esttab m0_t4earn m1_t4earn using $out/tableB1.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB1.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB1.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB1.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B1 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("National Local")
	fmt("%9.2fc") ylab("-0.3(0.1)0.2")
	fig("FigureB1") subfig("A");

#delimit cr

**# Figure B1 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("National Local")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB1") subfig("B");

#delimit cr
restore, not

********************************************************************************
							* Table B2 *
********************************************************************************
use $data/worker_matched, clear

gegen matched_deal_type = firstnm(merger), by(pairid year_prior)

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B2 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_deal_type == 0
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B2 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_deal_type == 0
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B2 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_deal_type == 1
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B2 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_deal_type == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;
local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Acquisition")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Merger")";

esttab m0_t4earn m1_t4earn using $out/tableB2.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB2.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB2.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB2.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B2 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("Acquisition Merger")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB2") subfig("A");

#delimit cr

**# Figure B2 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("Acquisition Merger")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB2") subfig("B");

#delimit cr
restore, not
********************************************************************************
							* Table B3 *
********************************************************************************
use $data/worker_matched, clear

merge 	m:1		DEAL Acquirer sac_syn naics 	using $data/same_market_firm,	keep(1 3) keepusing(same_market) nogen
replace	same_market			= .	if treated == 0 | t ~= -1

gegen matched_same_market	= firstnm(same_market),	by(pairid year_prior)

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B3 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_same_market == 0
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B3 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_same_market == 0
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B3 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_same_market == 1
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B3 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_same_market == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Across Market")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Within Market")";

esttab m0_t4earn m1_t4earn using $out/tableB3.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB3.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB3.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB3.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B3 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("AcrossMarket WithinMarket")
	fmt("%9.2fc") ylab("-0.3(0.1)0.2")
	fig("FigureB3") subfig("A");

#delimit cr

**# Figure B3 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("AcrossMarket WithinMarket")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB3") subfig("B");

#delimit cr
restore, not

#delimit ;
* To Take Out *;
global included_var2	"1.treated#1.ds_3 1.treated#1.ds_4 1.treated#1.ds_5 1.treated#1.ds_7 1.treated#1.ds_8 1.treated#1.ds_9 1.treated#1.ds_10 1.treated#1.ds_11 1.treated#1.ds_12";
forvalue h = 0/3{;
	esttab es`h'* using $out/FigureB3_`h'.csv, b se nostar noobs
	replace keep($included_var2) type;
};
#delimit cr

********************************************************************************
							* Table B4 *
********************************************************************************
use $data/worker_matched, clear

gsort -treated
gegen seq_market	= seq() if t == -1, by(sac_syn naics year)

merge 	m:1 	sac_syn naics year 		using $data/concentration, 		keep(1 3) keepusing(hhi_emp) nogen

** initial concentration: cz X industry
replace 	hhi_emp				= .	if treated == 0 | t ~= -1 | seq_market ~= 1
gquantiles 	hhi_emp_tmp	 	= hhi_emp, xtile n(5)
gegen 		hhi_emp_5 		= firstnm(hhi_emp_tmp), 	by(sac_syn naics year t)

gsort pairid year_prior -treated year
gegen 	matched_emp_5		= firstnm(hhi_emp_5), 		by(pairid year_prior)

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B4 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 1
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)' 
	estimates store es0_`v'
}


**# Table B4 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B4 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 5
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B4 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 5
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Q1")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Q5")";

esttab m0_t4earn m1_t4earn using $out/tableB4.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB4.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB4.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB4.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B4 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("Q1 Q5")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB4") subfig("A");

#delimit cr

**# Figure B4 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("Q1 Q5")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB4") subfig("B");

#delimit cr
restore, not

********************************************************************************
							* Table B5 *
********************************************************************************
use $data/worker_matched, clear

gsort -treated
gegen seq_market	= seq() if t == -1, by(sac_syn naics year)

merge 	m:1 	sac_syn naics year 		using $data/ghhi, 		keep(1 3) keepusing(ghhi_emp) nogen

** initial concentration: cz X industry
replace 	ghhi_emp		= .	if treated == 0 | t ~= -1 | seq_market ~= 1
gquantiles 	ghhi_emp_tmp	= ghhi_emp, xtile n(5)
gegen 		ghhi_emp_5 		= firstnm(ghhi_emp_tmp), 	by(sac_syn naics year t)

gsort pairid year_prior -treated year
gegen 	matched_emp_5		= firstnm(ghhi_emp_5), 		by(pairid year_prior)

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B5 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 1
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B5 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B5 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 5
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B5 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emp_5 == 5
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Q1")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Q5")";

esttab m0_t4earn m1_t4earn using $out/tableB5.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB5.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB5.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB5.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B5 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("Q1 Q5")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB5") subfig("A");

#delimit cr

**# Figure B5 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("Q1 Q5")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB5") subfig("B");

#delimit cr

#delimit ;
* To Take Out *;
global included_var2	"1.treated#1.ds_3 1.treated#1.ds_4 1.treated#1.ds_5 1.treated#1.ds_7 1.treated#1.ds_8 1.treated#1.ds_9 1.treated#1.ds_10 1.treated#1.ds_11 1.treated#1.ds_12";
forvalue h = 0/3{;
	esttab es`h'* using $out/FigureB5_`h'.csv, b se nostar noobs
	replace keep($included_var2) type;
};
#delimit cr

restore, not
********************************************************************************
							* Table B7 *
********************************************************************************
use $data/worker_matched, clear

** tradable vs. nontradable sector
gen 	tradable = (naics2 == 11 | naics2==21 | naics2 == 31 | naics2 == 55)
replace tradable =	.	if treated	==	0	|	t	~=	-1

gsort pairid year_prior -treated year
gegen matched_trade			= firstnm(tradable), 			by(pairid year_prior) 

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B7 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_trade == 1
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B7 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_trade == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B7 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_trade == 0
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B7 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_trade == 0
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Tradable")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Non-tradable")";

esttab m0_t4earn m1_t4earn using $out/tableB7.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB7.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB7.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB7.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# Figure B7 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("Tradable Nontradable")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB7") subfig("A");

#delimit cr

**# Figure B7 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("Tradable Nontradable")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB7") subfig("B");

#delimit cr
restore, not
********************************************************************************
							* Table B8 *
********************************************************************************
use $data/worker_matched, clear

** within industry vs. across industry 
merge	m:1 DEAL Acquirer 	using 	$data/first_mna, 	keep(1 3) keepusing(within_all) nogen
replace within_all =	.	if treated	==	0	|	t	~=	-1

gsort pairid year_prior -treated year
gegen matched_within 		= firstnm(within_all), 			by(pairid year_prior) 

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B8 - col 1, top half
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_within == 0
keep if present_at_firm == 1
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
foreach v in `varlist'{
	eststo es0_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m0_`v': lincomest $average_impact
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es0_`v'
}


**# Table B8 - col 2, top half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_within == 0
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es1_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m1_`v': lincomest $average_impact
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es1_`v'
}

**# Table B8 - col 1, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_within == 1
keep if present_at_firm == 1
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es2_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m2_`v': lincomest $average_impact
	
	estimates restore es2_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es2_`v'
}


**# Table B8 - col 2, bottom half
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_within == 1
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
foreach v in `varlist'{
	eststo es3_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo m3_`v': lincomest $average_impact
	
	estimates restore es3_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'  
	estimates store es3_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Stayers" "Movers") coeflabel((1) "Vertical M&A")";
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Horizontal M&A")";

esttab m0_t4earn m1_t4earn using $out/tableB8.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_t4earn es1_t4earn using $out/tableB8.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

esttab m2_t4earn m3_t4earn using $out/tableB8.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es2_t4earn es3_t4earn using $out/tableB8.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr
**# Figure B8 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es2_t4earn")
	colors("dkorange maroon")
	labels("VerticalM&A HorizontalM&A")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB8") subfig("A");

#delimit cr

**# Figure B8 - B
#delimit ;
make_eventstudy t4earn,
	models("es1_t4earn es3_t4earn")
	colors("dkorange maroon")
	labels("VerticalM&A HorizontalM&A")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB8") subfig("B");

#delimit cr
restore, not
********************************************************************************
							* Table B9 *
********************************************************************************
use $data/worker_matched, clear

replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)

gsort id year
** Moved Sector
replace 	moved_sec = . if treated == 0
replace 	moved_sec = . if t ~= min_t_moved

** Moved Industry
replace 	moved_ind		=	. if treated == 0
replace		moved_ind		=	. if t ~= min_t_moved

gsort pairid year_prior -treated year
gegen 		matched_moved_sec = firstnm(moved_sec), by(pairid year_prior)
gegen 		matched_moved_ind = firstnm(moved_ind), by(pairid year_prior)


*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)

label var y_t4earn 		"log(Earnings)"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"

**# Table B9 - col 1
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emoved == 1
replace t_moved = . if treated == 0
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

estimates clear
local varlist "t4earn"
local hetero "matched_moved_ind"
foreach v in `varlist'{
	eststo es0_`v': reghdfe	y_`v' `hetero'##treated##ds_*, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo treat0_`v': lincomest (1bn.treated#1bn.ds_7+1bn.treated#1bn.ds_8+1bn.treated#1bn.ds_9+1bn.treated#1bn.ds_10+1bn.treated#1bn.ds_11+1bn.treated#1bn.ds_12)/6
	
	estimates restore es0_`v'
	eststo interact0_`v': lincomest (1bn.`hetero'#1bn.treated#1bn.ds_7 + 1bn.`hetero'#1bn.treated#1bn.ds_8 + 1bn.`hetero'#1bn.treated#1bn.ds_9 + 1bn.`hetero'#1bn.treated#1bn.ds_10 + 1bn.`hetero'#1bn.treated#1bn.ds_11 + 1bn.`hetero'#1bn.treated#1bn.ds_12)/6
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 0
	estadd scalar ym0 = `r(mean)'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 1
	estadd scalar ym1 = `r(mean)' 
	summ `e(depvar)' if `hetero' == 0
	estadd scalar n0 = `r(N)'
	summ `e(depvar)' if `hetero' == 1
	estadd scalar n1 = `r(N)'
	estimates store es0_`v'
}

local varlist "t4earn"
foreach v in `varlist'{
	
	eststo es_ind0_`v': reghdfe y_`v' treated##ds_* if matched_moved_ind == 0,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es_ind0_`v'
	
	eststo es_ind1_`v': reghdfe y_`v' treated##ds_* if matched_moved_ind == 1,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es_ind1_`v'
}


**# Table B9 - col 2
restore, preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emoved == 1
replace t_moved = . if treated == 0
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

local varlist "t4earn"
local hetero "matched_moved_sec"
foreach v in `varlist'{
	eststo es1_`v': reghdfe	y_`v' `hetero'##treated##ds_*, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo treat1_`v': lincomest (1bn.treated#1bn.ds_7+1bn.treated#1bn.ds_8+1bn.treated#1bn.ds_9+1bn.treated#1bn.ds_10+1bn.treated#1bn.ds_11+1bn.treated#1bn.ds_12)/6
	
	estimates restore es1_`v'
	eststo interact1_`v': lincomest (1bn.`hetero'#1bn.treated#1bn.ds_7 + 1bn.`hetero'#1bn.treated#1bn.ds_8 + 1bn.`hetero'#1bn.treated#1bn.ds_9 + 1bn.`hetero'#1bn.treated#1bn.ds_10 + 1bn.`hetero'#1bn.treated#1bn.ds_11 + 1bn.`hetero'#1bn.treated#1bn.ds_12)/6
	
	estimates restore es1_`v'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 0
	estadd scalar ym0 = `r(mean)'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 1
	estadd scalar ym1 = `r(mean)' 
	summ `e(depvar)' if `hetero' == 0
	estadd scalar n0 = `r(N)'
	summ `e(depvar)' if `hetero' == 1
	estadd scalar n1 = `r(N)'
	estimates store es1_`v'
}

local varlist "t4earn"
foreach v in `varlist'{
	
	eststo es_sec0_`v': reghdfe y_`v' treated##ds_* if matched_moved_sec == 0,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es_sec0_`v'
	
	eststo es_sec1_`v': reghdfe y_`v' treated##ds_* if matched_moved_sec == 1,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es_sec1_`v'
}


#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("Industry" "Sector") coeflabel((1) "Post X Treated")";
local title2 "f collabels(none) plain noobs title("") nomtitles coeflabel((1) "Post X Treated X Moved From Industry")";
local title3 "f collabels(none) plain noobs title("") nomtitles coeflabel((1) "Post X Treated X Moved From Sector")";

esttab treat0_* treat1_* using $out/tableB9, 
replace `title1' csv
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(fmt(3) par) ) type;

esttab interact0_* using $out/tableB9, 
append `title2' csv
starlevel(* 0.10 ** 0.05 *** 0.01)
cells( b(star fmt(3)) se(fmt(3) par) ) type;

esttab interact1_* using $out/tableB9, 
append `title3' csv
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells("mean b(star fmt(3))" ". se(fmt(3) par)") type;

esttab es0_* es1_* using $out/tableB9,
append plain f collabels(none) keep("") nomtitles csv
stats(ym0 ym1 r2_a n0 n1, fmt(%9.2fc %9.2fc %9.3fc %9.0fc %9.0fc) label(
"Mean at t=-1 (Moved Within)"
"Mean at t=-1 (Moved Across)"
"Adj. R squared" 
"Worker-Year Obs. (Moved Within)"
"Worker-Year Obs. (Moved Across)")) type;

#delimit cr
**# Figure B9 - A
#delimit ;
make_eventstudy t4earn,
	models("es_ind0_t4earn es_ind1_t4earn")
	colors("dkorange maroon")
	labels("MovedWithinIndustry MovedAcrossIndustry")
	fmt("%9.2fc") ylab("-0.15(0.05)0.05")
	fig("FigureB9") subfig("A");

#delimit cr

**# Figure B9 - B
#delimit ;
make_eventstudy t4earn,
	models("es_sec0_t4earn es_sec1_t4earn")
	colors("dkorange maroon")
	labels("MovedWithinIndustry MovedAcrossIndustry")
	fmt("%9.2fc") ylab("-0.15(0.05)0.05")
	fig("FigureB9") subfig("B");

#delimit cr
restore, not
********************************************************************************
							* Table B10 *
********************************************************************************
use $data/worker_matched, clear

** fired vs. quit 
gsort pairid year_prior -treated year
gegen matched_fired			= firstnm(fired),				by(pairid year_prior)

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved

label var y_t4earn 		"log(Earnings)"
label var y_moved		"Transition"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"

**# Table B10 - col 1
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2	
keep if matched_acq == 0

estimates clear
local varlist "t4earn moved"
local hetero "matched_fired"
foreach v in `varlist'{
	eststo es0_`v': reghdfe	y_`v' `hetero'##treated##ds_*, a($FE) vce( cluster $clusters ) tol(1e-6)

	eststo treat0_`v': lincomest (1bn.treated#1bn.ds_7+1bn.treated#1bn.ds_8+1bn.treated#1bn.ds_9+1bn.treated#1bn.ds_10+1bn.treated#1bn.ds_11+1bn.treated#1bn.ds_12)/6
	
	estimates restore es0_`v'
	eststo interact0_`v': lincomest (1bn.`hetero'#1bn.treated#1bn.ds_7 + 1bn.`hetero'#1bn.treated#1bn.ds_8 + 1bn.`hetero'#1bn.treated#1bn.ds_9 + 1bn.`hetero'#1bn.treated#1bn.ds_10 + 1bn.`hetero'#1bn.treated#1bn.ds_11 + 1bn.`hetero'#1bn.treated#1bn.ds_12)/6
	
	estimates restore es0_`v'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 0
	estadd scalar ym0 = `r(mean)'
	summ `e(depvar)' if treated == 1 & t == -1 & `hetero' == 1
	estadd scalar ym1 = `r(mean)' 
	summ `e(depvar)' if `hetero' == 0
	estadd scalar n0 = `r(N)'
	summ `e(depvar)' if `hetero' == 1
	estadd scalar n1 = `r(N)'
	estimates store es0_`v'
}

#delimit ;

local title1 "f collabels(none) noobs title("") mtitle("log(Earnings)" "Transition") coeflabel((1) "Post X Treated")";	
local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "Post X Treated X Displaced")";

esttab treat0_* using $out/tableB10.csv, 
replace `title1'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab interact0_* using $out/tableB10.csv, 
append `title2'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

esttab es0_* using $out/tableB10.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym0 ym1 r2_a n0 n1, fmt(%9.2fc %9.2fc %9.3fc %9.0fc %9.0fc) label(
"Mean at t=-1 (Voluntary = 1)"
"Mean at t=-1 (Displaced = 1)"
"Adj. R squared" 
"Worker-Year Obs. (Voluntary = 1)"
"Worker-Year Obs. (Displaced = 1)")) type;

#delimit cr

local varlist "t4earn moved"
foreach v in `varlist'{
	
	eststo es0_`v': reghdfe y_`v' treated##ds_* if matched_fired == 0,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es0_`v'
	
	eststo es1_`v': reghdfe y_`v' treated##ds_* if matched_fired == 1,  a($FE) vce( cluster $clusters ) tol(1e-6)
	estimate restore es1_`v'
}

**# Figure B10 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es1_t4earn")
	colors("dkorange maroon")
	labels("Voluntary Involuntary")
	fmt("%9.2fc") ylab("-0.15(0.05)0.05")
	fig("FigureB10") subfig("A");

#delimit cr

**# Figure B10 - B
#delimit ;
make_eventstudy moved,
	models("es0_moved es1_moved")
	colors("dkorange maroon")
	labels("Voluntary Involuntary")
	fmt("%9.2fc") ylab("0.0(0.2)0.8")
	fig("FigureB10") subfig("B");

#delimit cr
restore, not

********************************************************************************
							* Table B11 *
********************************************************************************
use $data/worker_matched_w_tenure, clear

*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved

label var y_t4earn 			"log(Earnings)"
label var y_moved 			"Transition"

global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"

**# Table B11 - top panel
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

keep if matched_acq == 0

* first two cohort of movers
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2
keep if matched_tenure == 2 // 5 and 6 years

estimates clear
local varlist "t4earn moved"
foreach v in `varlist'{
	eststo es_`v': reghdfe y_`v' $ind_var, a($FE) vce( cluster $clusters ) tol(1e-6)
	
	eststo m_`v': lincomest $average_impact
	
	estimates restore es_`v'
	summ `e(depvar)' if treated == 1 & t == -1
	estadd scalar ym = `r(mean)'
	estimates store es_`v'
}

#delimit ;
local first = 1;

if `first'{;
	local replace_append "replace";
	local title "f collabels(none) noobs title("") mtitle("log(Earnings)" "Transition") coeflabel((1) "Workers Moving from Targets")";
	local first = 0;
};

esttab m_t4earn m_moved using $out/tableB11.csv,
`replace_append' `title'
starlevel(* 0.10 ** 0.05 *** 0.01) 
cells( b(star fmt(3)) se(par fmt(3)) ) type;

	
esttab es_t4earn es_moved using $out/tableB11.csv,
append plain f collabels(none) keep("") nomtitles 
stats(ym r2_a N, fmt(%9.2f %9.3f %9.0fc) label("Mean at t=-1" "Adj. R squared" "Worker-Year Obs.")) type;

#delimit cr

**# FigureB11 - A
#delimit ;
make_eventstudy t4earn,
	models("es_t4earn")
	colors("dkorange")
	labels("5-6YearsofTenure")
	fmt("%9.2fc") ylab("-0.12(0.04)0.08")
	fig("FigureB11") subfig("A");

#delimit cr

**# Figure 4 - B
#delimit ;
make_eventstudy moved,
	models("es_moved")
	colors("dkorange")
	labels("5-6YearsofTenure")
	fmt("%9.2fc") ylab("-0.1(0.2)0.7")
	fig("FigureB11") subfig("B");

#delimit cr

#delimit ;
* To Take Out *;
global included_var2	"1.treated#1.ds_3 1.treated#1.ds_4 1.treated#1.ds_5 1.treated#1.ds_7 1.treated#1.ds_8 1.treated#1.ds_9 1.treated#1.ds_10 1.treated#1.ds_11 1.treated#1.ds_12";
forvalue h = 0/0{;
	esttab es* using $out/FigureB11.csv, b se nostar noobs
	replace keep($included_var2) type;
};
#delimit cr

restore, not
********************************************************************************
							* Table B12 *
********************************************************************************
use $data/worker_matched, clear

** age variable
gen 	age_group = 0 if t1_age >= 20 & t1_age < 30
replace age_group = 1 if t1_age >= 30 & t1_age < 40
replace age_group = 2 if t1_age >= 40 & t1_age < 50
replace age_group = 3 if t1_age >= 50
replace age_group = . if treated == 0 | t ~= -1

gsort pairid year_prior -treated year
gegen matched_age    		= firstnm(age_group), 			by(pairid year_prior)


*** Dependent Variables ***
gen y_t4earn 		= log(total_wage)
gen y_moved			= moved

label var y_t4earn 			"log(Earnings)"
label var y_moved 			"Transition"


global FE 				"id i.t##i.naics"
global clusters 		"worker_id firm_id"
global ind_var 			"treated##ds_*"
global average_impact 	"(1.treated#1.ds_7 + 1.treated#1.ds_8 + 1.treated#1.ds_9 + 1.treated#1.ds_10 + 1.treated#1.ds_11 + 1.treated#1.ds_12)/6"

**# Table B12
preserve
gstats winsor y_*, replace cuts(5 95)  by(year)

*## sample selection
keep if matched_emoved == 1
replace t_moved = . if treated == 0
gegen min_t_moved = min(t_moved), by(pairid year_prior)
keep if min_t_moved == 1 | min_t_moved == 2
keep if matched_acq == 0


estimates clear
local varlist "t4earn moved"
forvalue h = 0/3{
	foreach v in `varlist'{
		eststo es`h'_`v': reghdfe y_`v' $ind_var if matched_age == `h', a($FE) vce( cluster $clusters ) tol(1e-6)

		eststo m`h'_`v': lincomest $average_impact
		
		estimates restore es`h'_`v'
		summ `e(depvar)' if treated == 1 & t == -1
		estadd scalar ym = `r(mean)'  
		estimates store es`h'_`v'
	}
}

#delimit ;
local first = 1;

forvalue h = 0/3{;
	if `first'{;
		local replace_append "replace";
		local title0 "f collabels(none) noobs title("") mtitle("log(Earnings)" "Transition") coeflabel((1) "20s")";
		local first = 0;
	};
	else  {;
		local replace_append "append";
		local title1 "f collabels(none) noobs title("") nomtitles coeflabel((1) "30s")";
		local title2 "f collabels(none) noobs title("") nomtitles coeflabel((1) "40s")";
		local title3 "f collabels(none) noobs title("") nomtitles coeflabel((1) "50+")";
	};

	esttab m`h'_t4earn m`h'_moved using "$out/tableB12.csv", 
	`replace_append' `title`h''
	starlevel(* 0.10 ** 0.05 *** 0.01) 
	cells( b(star fmt(3)) se(par fmt(3)) ) type;
	
	esttab es`h'_t4earn es`h'_moved using "$out/tableB12.csv", 
	append plain f collabels(none) keep("") nomtitles 
	stats(ym r2_a N, fmt(%9.2fc %9.3fc %9.0fc) label("Mean at t=-1" "Adj. R squared" "Firm-Year Obs.")) type;
};

#delimit cr

**# Figure B12 - A
#delimit ;
make_eventstudy t4earn,
	models("es0_t4earn es1_t4earn es2_t4earn es3_t4earn")
	colors("dkorange yellow maroon green")
	labels("20-30 30-40 40-50 50+")
	fmt("%9.2fc") ylab("-0.1(0.05)0.05")
	fig("FigureB12") subfig("A");

#delimit cr

**# Figure B12 - B
#delimit ;
make_eventstudy moved,
	models("es0_moved es1_moved es2_moved es3_moved")
	colors("dkorange yellow maroon green")
	labels("20-30 30-40 40-50 50+")
	fmt("%9.2fc") ylab("0.0(0.2)0.8")
	fig("FigureB12") subfig("B");

#delimit cr